<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客正文</title>
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/blog_content.css">
    <link rel="stylesheet" href="css/blog_mycomment.css">
    <link rel="stylesheet" href="editor.md/css/editormd.preview.min.css" />
    <script src="js/jquery.min.js"></script>
    <script src="editor.md/editormd.js"></script>
    <script src="editor.md/lib/marked.min.js"></script>
    <script src="editor.md/lib/prettify.min.js"></script>
    <script src="js/commom.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo.jpg" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="/blog/blog_list.html">主页</a>
        <a href="/blog/myblog_list.html">个人博客列表页</a>
        <a href="/blog/blog_add.html">写博客</a>
        <a href="/blog/mydraft_list.html">草稿箱</a>
        <a href="javascript:logout()">注销</a>
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 左侧个人信息 -->
        <div class="container-left">
            <div class="card">
                <img src="img/pop.jpg" class="avtar" alt="">
                <h3 id="username"></h3>
                <a href="http:www.github.com">github 地址</a>
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="counter">
                    <span id="artCount"></span>
                    <span>1</span>
                </div>
            </div>
        </div>
        <!-- 右侧内容详情 -->
        <div class="container-right">
            <div class="blog-content">
                <!-- 博客标题 -->
                <h3 id="title"></h3>
                <!-- 博客时间 -->
                <div class="date">发布时间：<span id="updatetime"></span> &nbsp;&nbsp;
                                阅读量：<span id="rcount"></span>    
                </div>
                <!-- 博客正文 -->
                <div id="editorDiv">

                </div>
               
            </div>
            <div class="upvote">
                <button onclick="upvote()">点赞</button>
                <span id="tag" class="tag"></span>
            </div>
           
            <div>
                <hr>
                <div class="user-face">
                    <img src="img/pop.jpg" alt="">
                </div>
                <div class="textarea-comment">
                    <textarea cols="80" rows="5" placeholder="发一条友善的评论" class="text-comment" id="text-comment"></textarea>
                    <button onclick="myComment()">发布评论</button>
                </div>

                <div id="comment-list">
                    
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript">
            var editormd;
            function initEdit(md){
                editormd = editormd.markdownToHTML("editorDiv", {
                markdown : md, // Also, you can dynamic set Markdown text
                // htmlDecode : true,  // Enable / disable HTML tag encode.
                // htmlDecode : "style,script,iframe",  // Note: If enabled, you should filter some dangerous HTML tags for website security.
                });
            }
            
            //文章详情
            function getArtDetail(id) {
                if(id == "") {
                    alert("非法参数");
                    return;
                }
                jQuery.ajax({
                    url: "/blog/art/detail",
                    type: "POST",
                    data: {"id":id},
                    success:function(result) {
                        if(result != null && result.code == 200) {
                            jQuery("#title").html(result.data.title);
                            jQuery("#updatetime").html(result.data.updatetime);
                            jQuery("#rcount").html(result.data.rcount+1);
                            initEdit(result.data.content);
                            //查询用户信息1
                            showUser(result.data.uid);
                        } else {
                            alert("查询失败，请重试！");
                        }
                    }
                });
            }
            getArtDetail(getUrlValue("id"));
            function showUser(id) {
                jQuery.ajax({
                    url: "/blog/user/getuserbyid",
                    type: "POST",
                    data:{"id":id},
                    success:function(result) {
                        if(result != null && result.code == 200 && result.data.id>0) {
                            if(result.data.nickname!=null) {
                                result.data.username = result.data.nickname;
                            }
                            jQuery("#username").text(result.data.username);
                            jQuery("#artCount").text(result.data.artCount);
                        } else {
                            alert("个人信息加载有误，请重试~");
                        }
                    }
                });  
            }

            //阅读量
            function updataRCount() {
                //先得到文章id
                var id = getUrlValue("id");
                if(id!="") {
                    jQuery.ajax({
                        url: "/blog/art/uprcount",
                        type: "POST",
                        data:{"id":id},
                        success:function(result) {
                            
                        }
                    });
                }

            }
            updataRCount();

            //发布评论
            function myComment() {
                var content = jQuery("#text-comment");
                // var content = jQuery("#aaa");
                //1、判空
                if(content.val() == "") {
                    alert("请输入评论内容");
                    content.focus();
                    return;
                } 
                //先得到文章id
                var id = getUrlValue("id");
                 if(id<1) {
                    alert("文章加载有误");
                    return;
                }
                //2、向后端发送请求
                jQuery.ajax({
                    url: "/blog/comment/makecomment",
                    type: "POST",
                    data: {"artid":id,"content":content.val()},
                    success:function(result) {
                        if(result != null && result.code == 200 && result.data == 1){
                            alert("评论成功");
                            location.href = location.href;
                        } else {
                            alert(result.msg);
                        }
                    }
                });

            }

            
            //显示已有的评论
            function queryComments() {
                //先得到文章id
                var id = getUrlValue("id");
                if(id<1) {
                    alert("文章加载有误");
                    return;
                }
                jQuery.ajax({
                    url: "/blog/comment/querycomments",
                    type: "POST",
                    data:{"artid":id},
                    success:function(result) {
                        if(result != null && result.code == 200 && result.data.length > 0) {
                            var commentlistDiv = "";
                            for( var i = 0;i<result.data.length;i++) {
                                var commentContent = result.data[i];
                                commentlistDiv += '<div class="comment">';
                                commentlistDiv +=   '<div class="username">'+commentContent.username+' '+commentContent.createtime+'</div>';
                                commentlistDiv +=   '<div class="comment-content">'+commentContent.content+'</div>';
                                commentlistDiv += '</div>';
                            }
                            jQuery("#comment-list").html(commentlistDiv);
                        } 
                    }
                });
            }
            queryComments();

            //点赞
            function upvote() {
                 //先得到文章id
                 var id = getUrlValue("id");
                 if(id<1) {
                    alert("文章加载有误");
                    return;
                }
                //向后端发送请求
                jQuery.ajax({
                    url: "/blog/vote/upvote",
                    type: "POST",
                    data: {"artid":id},
                    success:function(result) {
                        if(result != null && result.code == 200 && result.data.length >= 0) {
                            alert(result.data);
                            location.href = location.href;
                        } else {
                            alert(result.msg);
                        }
                    }
                });
            }
            //点赞数
            function voteCount() {
                //先得到文章id
                var id = getUrlValue("id");
                 if(id<1) {
                    alert("文章加载有误");
                    return;
                }
                //向后端发送请求
                jQuery.ajax({
                    url: "/blog/vote/votecount",
                    type: "POST",
                    data: {"artid":id},
                    success:function(result) {
                        if(result != null && result.code == 200 && result.data >= 0) {
                           jQuery("#tag").text(result.data);
                        } else {
                            alert(result.msg);
                        }
                    }
                });
            }
            voteCount();
    </script> 
</body>

</html>
